package com.myrock.zlbandy.gorock.HttpModel;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.myrock.zlbandy.gorock.HttpModel.Model.ImageInfo;
import com.myrock.zlbandy.gorock.HttpModel.Model.RockInfo;
import com.myrock.zlbandy.gorock.rock.z.MyRockAPI;

import java.util.ArrayList;

/**
 * Created by zlbandy on 2017/9/8.
 */

public abstract class ParseInfo {



    public static ArrayList<RockInfo> GetRock(String keyWord, int page) {
        ArrayList<RockInfo> rockInfos = new ArrayList<RockInfo>();
        try{
            if(keyWord!=null){
                PostHttp postHttp = new PostHttp();
                String response = postHttp.post(MyRockAPI.GTE_BY_NAME+page,keyWord);
                rockInfos = ParseArrayString2Rock(response);
            }
            else{
                GetHttp getHttp = new GetHttp();
                String response =  getHttp.run(MyRockAPI.INFO_LIST+page);
                rockInfos = ParseArrayString2Rock(response);
            }

        }
        catch (Exception ex){
            System.err.print(ex);
        }

        return rockInfos;
    }


    public static ArrayList<RockInfo> ClassifyPhoto(String photoBase){
        ArrayList<RockInfo> rockInfos = new ArrayList<RockInfo>();
        try{
            PostHttp postHttp = new PostHttp();
            String response = postHttp.classifyPost(MyRockAPI.CLASSIFY,photoBase);
            JsonParser parser = new JsonParser();
            JsonArray jsonArray = parser.parse(response).getAsJsonArray();
            for(int i=0;i<jsonArray.size();i++){
                GetHttp getHttp = new GetHttp();
                String url = MyRockAPI.GET_BYID+jsonArray.get(i).getAsJsonObject().get("id").toString().replace("\"","");
                String onerock =  getHttp.run(url);
                RockInfo rockInfo = ParseString2Rock(onerock);
                rockInfo.setProbability(Double.valueOf(jsonArray.get(i).getAsJsonObject().get("probability").toString()));
                rockInfos.add(rockInfo);
            }

        }catch (Exception ex){
            System.err.print(ex);
        }
       return rockInfos;
    }

    public static ArrayList<ImageInfo> GetImage(String id){
        ArrayList<ImageInfo> imageInfos = new ArrayList<ImageInfo>();
        try{
            GetHttp getHttp = new GetHttp();
            String response =  getHttp.run(MyRockAPI.GET_PIC_LIST+id);
            JsonParser parser = new JsonParser();
            JsonArray jsonArray = parser.parse(response).getAsJsonArray();
            for(int i = 0;i<jsonArray.size();i++){
                Gson gson = new Gson();
                ImageInfo imageInfo = gson.fromJson(jsonArray.get(i).toString(),ImageInfo.class);
                imageInfos.add(imageInfo);
            }

        }catch (Exception ex){
            ex.printStackTrace();
        }
        return imageInfos;
    }

    private static RockInfo ParseString2Rock(String result){
        Gson gson = new Gson();
        RockInfo rockInfo = gson.fromJson(result,RockInfo.class);
        GetHttp getHttp = new GetHttp();
        try{
            String presponse =  getHttp.run(MyRockAPI.IMAGE_ID_API+rockInfo.getId());
            rockInfo.setImagePath(MyRockAPI.IMAGE_API+presponse.replace("\"","")+".jpg");
        }catch (Exception ex){
            System.err.print(ex);
        }
        return rockInfo;
    }

    private static ArrayList<RockInfo> ParseArrayString2Rock(String result){
        GetHttp getHttp = new GetHttp();
        ArrayList<RockInfo> rockInfos = new ArrayList<>();
        try{

            JsonParser parser = new JsonParser();
            JsonArray jsonArray = parser.parse(result).getAsJsonArray();
            for(int i = 0;i<jsonArray.size();i++){
                Gson gson = new Gson();
                RockInfo rockInfo = gson.fromJson(jsonArray.get(i).toString(),RockInfo.class);
                String presponse =  getHttp.run(MyRockAPI.IMAGE_ID_API+rockInfo.getId());
                rockInfo.setImagePath(MyRockAPI.IMAGE_API+presponse.replace("\"","")+".jpg");
                rockInfos.add(rockInfo);
            }
        }catch (Exception ex){
            System.err.print(ex);
        }
        return rockInfos;
    }


}
